/**
 * @file swei_dbuff.c
 * @brief
 * @author  ()
 * @version 1.0
 * @date 2024-09-19
 *
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-19 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "include/port/swei_log.h"
#include "include/swei_dbuff.h"
#include "include/swei_cache.h"

#include "global.h"
#include "FreeRTOS.h"
#include "queue.h"

#define DBUFF_MAX_SIZE (128)
QueueHandle_t dbuff_queue = NULL;

/// @brief
/// @param dat
/// @return
int32_t swei_dbuff_peek(swei_datatype_t *dat)
{
    // 1. 队列创建
    if (NULL == dbuff_queue) {
        dbuff_queue = xQueueCreate(DBUFF_MAX_SIZE, sizeof(swei_datatype_t));
    }

    // 2. 队列有效性检查
    if (NULL == dbuff_queue) {
        log_e("dbuff queuehave not been created!\r\n");
        return -EIO;
    }

    if (pdPASS != xQueuePeek(dbuff_queue, dat, 0)) {
        return -EIO;
    }

    return SUCC;
}

/// @brief 待发送消息存入发送缓存区
/// @param dat 待发送消息数据结构
/// @return
int32_t swei_dbuff_push(swei_datatype_t *dat)
{
    // 1. 队列创建
    if (NULL == dbuff_queue) {
        dbuff_queue = xQueueCreate(DBUFF_MAX_SIZE, sizeof(swei_datatype_t));
    }

    // 2. 队列有效性检查
    if (NULL == dbuff_queue) {
        log_e("dbuff queuehave not been created!\r\n");
        return -EIO;
    }

    // 3. 入参有效性判断
    if (NULL == dat) {
        log_e("dbuff queuehave not been created!\r\n");
        return -EIO;
    }

    // 3. 消息入队列
    if (pdPASS != xQueueSendToBack(dbuff_queue, dat, 0)) {
        return -EIO;
    }

    return SUCC;
}

/// @brief 从buffer中取出dat节点，常规dat节点应该是在队列首部，
///        但是考虑队列满后最后消息被出队的情况，优先pick后id相同再出队
/// @param dat
/// @return
int32_t swei_dbuff_pop(swei_datatype_t *dat, uint8_t is_match)
{
    swei_datatype_t temp = {0};

    // 1. 队列创建
    if (NULL == dbuff_queue) {
        dbuff_queue = xQueueCreate(DBUFF_MAX_SIZE, sizeof(swei_datatype_t));
    }

    // 2. 队列有效性检查
    if (NULL == dbuff_queue || NULL == dat) {
        log_e("dbuff queueor dat have not been created!\r\n");
        return -EIO;
    }

    // 3. 消息出队列
    if (is_match) {
        // 3.1 消息匹配后出队
        if (pdPASS != xQueuePeek(dbuff_queue, &temp, 0)) {
            return -EIO;
        }

        if (dat->data_id == temp.data_id) {
            if (pdPASS != xQueueReceive(dbuff_queue, dat, 0)) {
                return -EIO;
            }

            return SUCC;
        }
    } else {
        // 3.2 消息出队列
        if (pdPASS != xQueueReceive(dbuff_queue, &dat, 0)) {
            return -EIO;
        }
    }

    return SUCC;
}

/// @brief 判断dbuff是否为空
/// @param
/// @return
bool swei_dbuff_is_empty(void)
{
    if (NULL == dbuff_queue) {
        return true;
    }

    if (pdFAIL == uxQueueMessagesWaiting(dbuff_queue))
        return true;

    return false;
}

/// @brief 判断dbuff是否满
/// @param
/// @return
bool swei_dbuff_is_full(void)
{
    if (NULL == dbuff_queue) {
        return false;
    }

    if (pdFAIL == uxQueueSpacesAvailable(dbuff_queue))
        return true;

    return false;
}
